package com.panshi.controller;


import com.google.code.kaptcha.Producer;


import com.panshi.UserLoginPort;
import com.panshi.api.util.CommonResult;
import com.panshi.api.vo.UserLoginVo;




import com.panshi.bean.BeiUser;
import com.panshi.api.util.CommonResult;
import com.panshi.api.vo.UserLoginVo;
import com.panshi.common.CaptchaUtils;
import com.panshi.common.EmailPhoneUtill;
import com.panshi.service.BeiUserService;

import com.panshi.api.util.RedisUtil;
import com.panshi.api.util.ResultConstant;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.mail.MessagingException;
import javax.ws.rs.HEAD;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 用户表  前端控制器
 * </p>
 *
 * @author 小伍
 * @since 2020-08-01
 */
@RestController
//@RequestMapping("/bei-user")
@Api(tags = "用户登录接口")
@Slf4j
public class BeiUserController implements UserLoginPort {


    @Autowired
    private BeiUserService beiUserService;//用户service层

    @Autowired
    private Producer producer; //验证码类

   // @Autowired
    //private CommonResult commonResult; //封装返回结果类

    @Autowired
    private CaptchaUtils captchaUtils;

    @Autowired
    private EmailPhoneUtill emailPhoneUtill;

    @Autowired
    private RedisUtil redisUtil;//redis

    @Value("${server.servlet.session.timeout}")
    private Integer timeout;






    @GetMapping("/user/{uid}")
    @ApiOperation("根据用户id查询信息")
    @ApiImplicitParam(name="uid",value = "用户id",required = true)
    public BeiUser queryuserbyuid(@PathVariable("uid") String uid)
    {

        return beiUserService.queryUserByuid(uid);
    }



    @PostMapping("/user/login")
    @ApiOperation("用户登录接口")
    public CommonResult userlogin(@RequestBody  UserLoginVo beiUser){
        log.info("用户进来了{}",beiUser);
        String auths="";
        boolean matches = beiUser.getIphone().matches("^[1][3,4,5,7,8,9][0-9]{9}$");
        if(!matches){
            beiUser.setEmail(beiUser.getIphone());
            beiUser.setIphone(null);
        }
        BeiUser beiUser1 = beiUserService.queryUserBype(beiUser);
        CommonResult commonResult= new CommonResult<>();
        //判断当前用户是否存在
        if(beiUser1==null){
            commonResult.setCode(ResultConstant.AUTH_CODE);
            commonResult.setMsg(ResultConstant.ACCOUNTS_MES);
            return commonResult;
        }

        if(!StringUtils.isEmpty(beiUser.getIphone())){ //输入电话号码
            if(!beiUser1.getIphone().equals(beiUser.getIphone())){ //判断电话号码是否正确
                commonResult.setCode(ResultConstant.PHONE_CODE);
                commonResult.setMsg(ResultConstant.EMAIL_MES);
                return commonResult;
            }
        }else{//输入邮箱
            if(!beiUser1.getEmail().equals(beiUser.getEmail())){ //判断邮箱是否正确
                commonResult.setCode(ResultConstant.EMAIL_CODE);
                commonResult.setMsg(ResultConstant.EMAIL_MES);
                return commonResult;
            }
        }
        if(StringUtils.isEmpty(beiUser.getPassword())){
            Integer o=0;
            if(!StringUtils.isEmpty(beiUser.getEmail())){
             o = (Integer) redisUtil.get(beiUser.getEmail() + ":" + beiUser.getAuth());
            }else{
                o = (Integer) redisUtil.get(beiUser.getIphone()+":"+beiUser.getAuth());
            }
            if(!String.valueOf(o).equals(beiUser.getAuth())){
                commonResult.setCode(ResultConstant.AUTH_CODE);
                commonResult.setMsg(ResultConstant.AUTH_MES);
                return commonResult;
            }
        }else{
            if(!beiUser1.getPassword().equals(beiUser.getPassword())){ //判断密码是否正确
                commonResult.setCode(ResultConstant.PASSWORD_CODE);
                commonResult.setMsg(ResultConstant.PASSWORD_MES);
                return commonResult;
            }
        }
        CommonResult<Integer> beiUserCommonResult = new CommonResult<>();
        beiUserCommonResult.setData(beiUser1.getUid());
        return beiUserCommonResult;
    }


    @PostMapping("/user/getep")
    @ApiOperation("发送邮箱 电话验证码接口")
    public Map<String, Object> getemail(@RequestBody String iphone_email) throws MessagingException {
        boolean matches = iphone_email.matches("^[1][3,4,5,7,8,9][0-9]{9}$");

        System.out.println(iphone_email);

        if(matches){
            emailPhoneUtill.getiphone(iphone_email);
        }else{
            emailPhoneUtill.getemail(iphone_email);
        }
        Map<String, Object> map =new  HashMap<>();
        map.put("time",timeout);
        map.put("code","101");
        map.put("msg","验证码发送成功");
        return  map;
    }


    @PutMapping("/user/register")
    @ApiOperation("用户注册接口")
    public CommonResult<String> register(@RequestBody UserLoginVo userLoginVo){
        CommonResult<String> commonResult= new CommonResult<>();
        boolean matches = userLoginVo.getIphone().matches("^[1][3,4,5,7,8,9][0-9]{9}$");
        if(!matches){
            userLoginVo.setEmail(userLoginVo.getIphone());
            userLoginVo.setIphone(null);
        }
        BeiUser beiUser1 = beiUserService.queryUserBype(userLoginVo);
        //判断当前用户是否存在
        if(beiUser1!=null){
            commonResult.setCode(ResultConstant.USER_CODE);
            commonResult.setMsg(ResultConstant.USER_MES);
            return commonResult;
        }
        Integer o=0;
        if(!StringUtils.isEmpty(userLoginVo.getEmail())){
            o = (Integer) redisUtil.get(userLoginVo.getEmail() + ":" + userLoginVo.getAuth());
        }else{
            o = (Integer) redisUtil.get(userLoginVo.getIphone()+":"+userLoginVo.getAuth());
        }
        if(!String.valueOf(o).equals(userLoginVo.getAuth())){
            commonResult.setCode(ResultConstant.AUTH_CODE);
            commonResult.setMsg(ResultConstant.AUTH_MES);
            return commonResult;
        }

        beiUserService.addUser(userLoginVo);
        CommonResult<String> c =new CommonResult<>();
        BeiUser beiUser = beiUserService.queryUserBype(userLoginVo);
        c.setToken(String.valueOf(beiUser.getUid()));
        return c;
    }

    @PostMapping("/user/forgetpwd")
    @ApiOperation("用户忘记密码接口")
    public CommonResult forgetpwd(@RequestBody UserLoginVo userLoginVo){
        CommonResult commonResult= new CommonResult<>();
        BeiUser beiUser1 = beiUserService.queryUserBype(userLoginVo);
        //判断当前用户是否存在
        if(beiUser1==null){
            commonResult.setCode(ResultConstant.AUTH_CODE);
            commonResult.setMsg(ResultConstant.ACCOUNTS_MES);
            return commonResult;
        }
        Integer o=0;
        if(!StringUtils.isEmpty(userLoginVo.getEmail())){
            o = (Integer) redisUtil.get(userLoginVo.getEmail() + ":" + userLoginVo.getAuth());
        }else{
            o = (Integer) redisUtil.get(userLoginVo.getIphone()+":"+userLoginVo.getAuth());
        }
        if(!String.valueOf(o).equals(userLoginVo.getAuth())){
            commonResult.setCode(ResultConstant.AUTH_CODE);
            commonResult.setMsg(ResultConstant.AUTH_MES);
            return commonResult;
        }
        BeiUser beiUser2=new BeiUser();
        //assert false;
        beiUser2.setUid(beiUser1.getUid());
        beiUser2.setPassword(userLoginVo.getPassword());
        beiUserService.updateUser(beiUser2);
        return new CommonResult<>();
    }


}

